package com.example.hyg_veintype.activity.result;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hyg_veintype.R;
import com.example.hyg_veintype.data.local.analyseLocalBean.PluseDataWraper;
import com.example.hyg_veintype.activity.result.adapter.ChartAdapter;
import com.example.hyg_veintype.activity.result.adapter.VeinTypeSimpleNameAdapter;
import com.example.hyg_veintype.data.local.chart.ChartBean;
import com.example.hyg_veintype.view.veinTypeImgParamsDialog.VeinTypeImgParamsDialog;
import com.example.hyg_veintype.base.BaseVeinTypeActivity;
import com.example.hyg_veintype.base.baseadapter.BaseRecyclerAdapter;
import com.example.hyg_veintype.utils.ReadPluseTypeUtil;
import com.example.hyg_veintype.data.local.pluseType.PluseTypeBean;
import com.example.hyg_veintype.data.local.pluseType.RECORDSBean;
import com.example.pulseanalyse.veinPoint.myClassPluseAnalysisSD;
import com.example.pulseanalyse.veinType.MainWindow;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;


import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author: yyw
 * time: 2020/12/16 14:42
 * desc: 根据检测数据得出结果
 */
public class ResultActivity extends BaseVeinTypeActivity {
    private static final String ANALYSEBEAN = "analyseBean";

    TextView tvPulseRate;
    RecyclerView rvSimpleName;
    TextView tvVeintypeMsg;
    TextView tvVeintypeMsgDetail;
    RecyclerView rlChart;

    //    AnalyseBean analyseBean;//脉点分析结果实体类
//    DataUtils dataUtils;//获取最终脉型的工具类
    VeinTypeSimpleNameAdapter simpleNameAdapter;//脉型简称分析结果列表适配器
    ChartAdapter chartAdapter;//最佳脉图图表适配器
    List<ChartBean> list = new ArrayList<>();//最佳脉图数据
    VeinTypeImgParamsDialog dialog;//脉图参数对话框
    static List<PluseDataWraper> pluseDataWraperList;

    public static void start(Context context, List<PluseDataWraper> mPluseDataWraperList) {
        pluseDataWraperList = new ArrayList<>();
        for (PluseDataWraper pluseDataWraper : mPluseDataWraperList) {
            //获取检测完成的数据
            if (pluseDataWraper.getStatus() == 2) {
                pluseDataWraperList.add(pluseDataWraper);
            }
        }
        Intent starter = new Intent(context, ResultActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.veintype_activity_result;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tvTitle.setText("脉型诊断");
        lnTopRight.setVisibility(View.VISIBLE);
        imgRight.setVisibility(View.GONE);
        tvRight.setText("完成");
        initView();
        initData();
        initAdapter();
        //脉图参数对话框
        dialog = new VeinTypeImgParamsDialog(this, list);
        chartAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int viewType, Object data, int position) {
                dialog.scrollToIndex(position);
                dialog.show();
            }
        });
    }

    private void initView() {
        tvPulseRate = findViewById(R.id.tv_pulse_rate);
        rvSimpleName = findViewById(R.id.rv_simple_name);
        tvVeintypeMsg = findViewById(R.id.tv_veintype_msg);
        tvVeintypeMsgDetail = findViewById(R.id.tv_veintype_msg_detail);
        rlChart = findViewById(R.id.rl_chart);
    }


    private void initData() {
        //进行脉型分析
        for (int i = 0; i < pluseDataWraperList.size(); i++) {
            MainWindow window = new MainWindow();
            myClassPluseAnalysisSD fuSd = pluseDataWraperList.get(i).getFuZhongChenWraper().getFuSd();
            myClassPluseAnalysisSD zhongSd = pluseDataWraperList.get(i).getFuZhongChenWraper().getZhongSd();
            myClassPluseAnalysisSD chenSd = pluseDataWraperList.get(i).getFuZhongChenWraper().getChenSd();
            window.getPluseResult(fuSd, zhongSd, chenSd);
            pluseDataWraperList.get(i).getFuZhongChenWraper().setMainWindow(window);
        }

        int pulseRate = 0;
        int count = 0;
        List<zuoOrYouVeintype> positionVeinTypeList = new ArrayList<>();
        List<String> noRepeatList = new ArrayList<>();
        for (int i = 0; i < pluseDataWraperList.size(); i++) {
            PluseDataWraper pluseDataWraper = pluseDataWraperList.get(i);
            if (pluseDataWraper.getFuZhongChenWraper().getMainWindow() != null) {
                count++;
                pulseRate = pulseRate + pluseDataWraper.getFuZhongChenWraper().getMainWindow().AvePulseRate;
                list.add(new ChartBean(pluseDataWraper.getSimpleName(),
                        pluseDataWraper.getFuZhongChenWraper().getMainWindow(),
                        pluseDataWraper.getFuZhongChenWraper().getFuSd(),
                        pluseDataWraper.getFuZhongChenWraper().getZhongSd(),
                        pluseDataWraper.getFuZhongChenWraper().getChenSd()));
                positionVeinTypeList.add(new zuoOrYouVeintype(pluseDataWraper.getSimpleName(), pluseDataWraper.getFuZhongChenWraper().getMainWindow().AvePulseType));
                String[] split = pluseDataWraper.getFuZhongChenWraper().getMainWindow().AvePulseType.split(",");
                for (String s : split) {
                    noRepeatList.add(s);
                }
            }
        }
        /************** 脉率处理 **************/
        if (count != 0) {
            tvPulseRate.setText(pulseRate / count + "");
        }
        /************** 脉型处理 **************/
        //去重操作
        List<String> newNoRepeatList = new ArrayList<String>(new LinkedHashSet<String>(noRepeatList));
        //全部脉数据
        String allType = ReadPluseTypeUtil.getPluseType(this);
        PluseTypeBean pluseTypeBean = new Gson().fromJson(allType, PluseTypeBean.class);
        List<RECORDSBean> recordsList = pluseTypeBean.getRECORDS();
        //诊断脉型结果解析文字(左右手都包含)
        List<RECORDSBean> resultList = new ArrayList<>();
        for (int i = 0; i < newNoRepeatList.size(); i++) {
            for (int j = 0; j < recordsList.size(); j++) {
                if (recordsList.get(j).getPluseName().contains(newNoRepeatList.get(i))) {
                    resultList.add(recordsList.get(j));
                }
            }
        }

        setVeinTypeUI(newNoRepeatList);

        String resultMsg = "";
        String resultMsgDetail = "";
        //左手和右手的寸、关、尺的脉型
        if (!positionVeinTypeList.isEmpty()) {
            for (zuoOrYouVeintype zuoOrYouVeintype : positionVeinTypeList) {
                resultMsg = resultMsg + zuoOrYouVeintype.getPosition() + "为" + zuoOrYouVeintype.getVeinType() + "脉\n";
            }
        }
        if (!resultList.isEmpty()) {
            for (RECORDSBean recordsBean : resultList) {
                resultMsgDetail = resultMsgDetail + recordsBean.getPluseName() + ":" + recordsBean.getPluseInfo() + "\n";
            }
        }
        tvVeintypeMsg.setText(resultMsg.trim());
        tvVeintypeMsgDetail.setText(resultMsgDetail.trim());

    }

    /**
     * 最佳脉型列表适配器
     */
    private void initAdapter() {
        try {
            chartAdapter = new ChartAdapter(list, this);
            rlChart.setLayoutManager(new GridLayoutManager(this, 2));
            rlChart.setAdapter(chartAdapter);
        } catch (Exception e) {

        }
    }

    /**
     * 设置检测脉型结果（如：数、弱、微等）
     *
     * @param list
     */
    private void setVeinTypeUI(List<String> list) {
        simpleNameAdapter = new VeinTypeSimpleNameAdapter(list, this);
        rvSimpleName.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        rvSimpleName.setAdapter(simpleNameAdapter);
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 左手/右手脉型
     */
    @NoArgsConstructor
    @Data
    private class zuoOrYouVeintype {
        private String position;
        private String veinType;

        public zuoOrYouVeintype(String position, String veinType) {
            this.position = position;
            this.veinType = veinType;
        }
    }
}
